package com.example.controller;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.example.listenanddriveproject.R;

public class HSVFragment extends Fragment {
	
	private Context context;
	private TextView textMin1;
	private TextView textMax1;
	private TextView textMin2;
	private TextView textMax2;
	private SeekBar seekBarMin1;
	private SeekBar seekBarMax1;
	private SeekBar seekBarMin2;
	private SeekBar seekBarMax2;
	private Button button;
	private int progressMin1;
	private int progressMax1;
	private int progressMin2;
	private int progressMax2;
	
	public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		
		View rootView = inflater.inflate(R.layout.fragment_hsv, container, false);
		
		context = this.getActivity();
		
		textMin1 = (TextView) rootView.findViewById(R.id.textViewMin1);
		textMin1.setText(String.valueOf(MainActivity.settings.getHsv().getMin1()));
		
		seekBarMin1=(SeekBar) rootView.findViewById(R.id.seekBarZoom);
		seekBarMin1.setProgress(MainActivity.settings.getHsv().getMin1());
		seekBarMin1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {       

		    @Override       
		    public void onStopTrackingTouch(SeekBar seekBar) {      
		        // TODO Auto-generated method stub      
		    }       
	
		    @Override       
		    public void onStartTrackingTouch(SeekBar seekBar) {     
		        // TODO Auto-generated method stub      
		    }       
	
		    @Override       
		    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 
		    	textMin1.setText(String.valueOf(progress));
		    	progressMin1=progress;
		    }       
	    });
		
		textMax1 = (TextView) rootView.findViewById(R.id.textViewMax1);
		textMax1.setText(String.valueOf(MainActivity.settings.getHsv().getMax1()));
		
		seekBarMax1=(SeekBar) rootView.findViewById(R.id.SeekBarMax1);
		seekBarMax1.setProgress(MainActivity.settings.getHsv().getMax1());
		seekBarMax1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {       

		    @Override       
		    public void onStopTrackingTouch(SeekBar seekBar) {      
		        // TODO Auto-generated method stub      
		    }       
	
		    @Override       
		    public void onStartTrackingTouch(SeekBar seekBar) {     
		        // TODO Auto-generated method stub      
		    }       
	
		    @Override       
		    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		    	textMax1.setText(String.valueOf(progress));
		    	progressMax1=progress;
		    }       
	    });
		
		textMin2 = (TextView) rootView.findViewById(R.id.textViewMin2);
		textMin2.setText(String.valueOf(MainActivity.settings.getHsv().getMin2()));
		
		seekBarMin2=(SeekBar) rootView.findViewById(R.id.seekBarMin2);
		seekBarMin2.setProgress(MainActivity.settings.getHsv().getMin2());
		seekBarMin2.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {       

		    @Override       
		    public void onStopTrackingTouch(SeekBar seekBar) {      
		        // TODO Auto-generated method stub      
		    }       
	
		    @Override       
		    public void onStartTrackingTouch(SeekBar seekBar) {     
		        // TODO Auto-generated method stub      
		    }       
	
		    @Override       
		    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		    	textMin2.setText(String.valueOf(progress));
		    	progressMin2=progress;
		    }       
	    });
		
		textMax2 = (TextView) rootView.findViewById(R.id.textViewMax2);
		textMax2.setText(String.valueOf(MainActivity.settings.getHsv().getMax2()));
		
		seekBarMax2=(SeekBar) rootView.findViewById(R.id.seekBarMax2);
		seekBarMax2.setProgress(MainActivity.settings.getHsv().getMax2());
		seekBarMax2.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {       

		    @Override       
		    public void onStopTrackingTouch(SeekBar seekBar) {      
		        // TODO Auto-generated method stub      
		    }       
	
		    @Override       
		    public void onStartTrackingTouch(SeekBar seekBar) {     
		        // TODO Auto-generated method stub      
		    }       
	
		    @Override       
		    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		    	textMax2.setText(String.valueOf(progress));
		    	progressMax2=progress;
		    }       
	    });
		
		button = (Button) rootView.findViewById(R.id.apply1);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	MainActivity.settings.getHsv().setMin1(progressMin1);
            	MainActivity.settings.getHsv().setMax1(progressMax1);
            	MainActivity.settings.getHsv().setMin2(progressMin2);
            	MainActivity.settings.getHsv().setMax2(progressMax2);
            	MainActivity.settings.saveArray(context);
            }
        });
		
		return rootView;
	}
	
	/***
	 * refresh the UI when the reset is called
	 */
	@Override
	public void onResume(){
		super.onResume();
		
		if(MainActivity.settings.getHsv().isRefresh()){
			textMin1.setText(String.valueOf(MainActivity.settings.getHsv().getMin1()));
			seekBarMin1.setProgress(MainActivity.settings.getHsv().getMin1());
			
			textMax1.setText(String.valueOf(MainActivity.settings.getHsv().getMax1()));
			seekBarMax1.setProgress(MainActivity.settings.getHsv().getMax1());
			
			textMin2.setText(String.valueOf(MainActivity.settings.getHsv().getMin2()));
			seekBarMin2.setProgress(MainActivity.settings.getHsv().getMin2());
			
			textMax2.setText(String.valueOf(MainActivity.settings.getHsv().getMax2()));
			seekBarMax2.setProgress(MainActivity.settings.getHsv().getMax2());
			
			MainActivity.settings.getHsv().setRefresh(false);
		}
	}

}
